<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>自动发放智慧幣规则管理</title>
    <!-- ICON -->
    <link href="/frame/images/favicon.ico" type="image/ico" rel="shortcut icon">
    <!-- CSS -->
    <link rel="stylesheet" href="/lib/layuiadmin/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/lib/layuiadmin/css/admin.css" media="all" />
    <style>
        .layui-form-pane .layui-form-label{
            width: 150px;
        }
        .layui-form-pane .layui-input-block {
            margin-left: 150px;
        }
        .layui-table-cell {
            line-height: 15px !important;
            vertical-align: middle;
            /*height: auto;*/
            overflow: visible;
            text-overflow: inherit;
            white-space: normal;
        }
    </style>
</head>
<body layadmin-themealias="default">
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <div class="layui-col-md2">
                    <!--<form class="layui-form" style="float: left;">-->
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-warm" lay-submit lay-filter="addBtn" title="新增">
                            <i class="layui-icon">&#xe61f;</i> 新增
                        </button>
                    </div>
                </div>
                <div class="layui-col-md10">
                    <form class="layui-form" id="queryForm" style="float: right;">
                                                <div class="layui-inline">
                                                    <input type="text" name="ny" id="ny" title="年月" placeholder="请输入，格式：yyyyMM" autocomplete="off" class="layui-input">
                                                    <span style="color:red">选择本月触发上月份的代金券发放，允许重复发放</span>
                                                </div>
                        <!--                        <div class="layui-inline">-->
                        <!--							<input type="text" name="mmmc" title="名称" placeholder="请输入名称" autocomplete="off" class="layui-input">-->
                        <!--						</div>-->
                        <button class="layui-btn layui-btn-warm" lay-submit lay-filter="enableJobBtn" title="代金券发放">
                            <i class="layui-icon">&#xe61f;</i>手动触发
                        </button>
                        <button class="layui-btn layui-btn-blue" lay-submit lay-filter="query">
                            <i class="layui-icon">&#xe615;</i> 查询
                        </button>

                        <!--                        <button class="layui-btn layui-btn-blue" lay-submit lay-filter="importxls" title="导入xls">-->
                        <!--                            <i class="layui-icon">&#xe615;</i> 导入xls-->
                        <!--                        </button>-->
                        <!--                        <button class="layui-btn layui-btn-blue" lay-submit lay-filter="exportxls" title="导出xls">-->
                        <!--                            <i class="layui-icon">&#xe615;</i> 导出xls-->
                        <!--                        </button>-->

                        <!--<button class="layui-btn" lay-submit lay-filter="crontimebtn2">-->
                        <!--<i class="layui-icon">&#xe615;</i> cron-->
                        <!--</button>-->
                    </form>
                </div>
            </div>
            <hr class="layui-bg-gray">
            <div class="layui-row">
                <div class="layui-col-md12">
                    <!-- 表格 -->
                    <table id="table" lay-filter="table"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 操作 -->
<script type="text/html" id="operationBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 状态操作 -->
<!--<script type="text/html" id="taskstatus">-->
<!--	<input type="checkbox" id="{{d.id}}" name="taskstatus" value="{{d.taskstatus}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="switchSysTaskStatus" {{ d.taskstatus == 1 ? 'checked' : '' }}>-->
<!--</script>-->

<!--<script type="text/html" id="triggerstatus">-->
<!--	<input type="checkbox" id="{{d.id}}" name="triggerstatus" value="{{d.triggerstatus}}" lay-skin="switch" lay-text="正常|停止" lay-filter="switchTriggerStatus" {{ d.triggerstatus == 1 ? 'checked' : '' }}>-->
<!--</script>-->
<!-- 状态操作 -->

<!--<script type="text/html" id="triggertype">-->
<!--	<input type="checkbox" id="{{d.id}}" name="triggertype" value="{{d.triggertype}}" lay-skin="switch" lay-text="Cron|Simple" lay-filter="switchTriggerType" {{ d.triggertype == 1 ? 'checked' : '' }}>-->
<!--</script>-->

<!-- 新增 -->
<script type="text/html" id="addForm">
    <form class="layui-form layui-form-pane">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-md12">
                        <div class="layui-form-item"><label class="layui-form-label">活动代码</label><div class="layui-input-block"><input type="text" name="hddm" autocomplete="off" placeholder="请输入活动代码" class="layui-input"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">活动名称</label><div class="layui-input-block"><input type="text" name="hdmc" autocomplete="off" placeholder="请输入活动名称" class="layui-input"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">奖项设置</label><div class="layui-input-block"><input type="text" name="jxsz" autocomplete="off" placeholder="请输入奖项设置" class="layui-input"></div></div>
                        <!--<div class="layui-form-item"><label class="layui-form-label">活动周期</label><div class="layui-input-block"><input type="text" name="hdzq" autocomplete="off" placeholder="请输入活动周期" class="layui-input"></div></div>-->
                        <!--<div class="layui-form-item"><label class="layui-form-label">积分名次</label><div class="layui-input-block"><input type="text" name="jfmc" autocomplete="off" placeholder="请输入积分名次" class="layui-input"></div></div>-->
                        <div class="layui-form-item"><label class="layui-form-label">人数</label><div class="layui-input-block"><input type="text" name="pnum" autocomplete="off" placeholder="请输入人数" class="layui-input"></div></div>
                        <!--<div class="layui-form-item"><label class="layui-form-label">发放时间（日）</label><div class="layui-input-block"><input type="text" name="fftime" autocomplete="off" placeholder="请输入发放时间（日）" class="layui-input"></div></div>-->
                        <div class="layui-form-item"><label class="layui-form-label">价值</label><div class="layui-input-block"><input type="text" name="price" autocomplete="off" placeholder="请输入价值" class="layui-input"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">排序号</label><div class="layui-input-block"><input type="text" name="orderno" autocomplete="off" placeholder="请输入排序号" class="layui-input"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">备注</label><div class="layui-input-block"><input type="text" name="bz" autocomplete="off" placeholder="请输入备注" class="layui-input"></div></div>
                    </div>
                    <div class="hidden-attribute">
                        <button class="layui-btn submit" lay-submit="" lay-filter="add">新增</button>
                        <button class="layui-btn reset" type="reset">重置</button>
                    </div>
                </div>
                <!--<input class="hidden-attribute" name="avatar">-->
            </div>
        </div>
    </form>
</script>

<!-- 详情 -->
<script type="text/html" id="view">
    <table class="layui-table" style="width: 680px; margin: 10px 10px 0px 10px;">
        <tr><td class="table-td-title">活动代码</td><td width="465">{{d.hddm||''}}</td></tr>
        <tr><td class="table-td-title">活动名称</td><td width="465">{{d.hdmc||''}}</td></tr>
        <tr><td class="table-td-title">奖项设置</td><td width="465">{{d.jxsz||''}}</td></tr>
        <!--<tr><td class="table-td-title">活动周期</td><td width="465">{{d.hdzq||''}}</td></tr>-->
        <!--<tr><td class="table-td-title">积分名次</td><td width="465">{{d.jfmc||''}}</td></tr>-->
        <tr><td class="table-td-title">人数</td><td width="465">{{d.pnum||''}}</td></tr>
        <!--<tr><td class="table-td-title">发放时间（日）</td><td width="465">{{d.fftime||''}}</td></tr>-->
        <tr><td class="table-td-title">价值</td><td width="465">{{d.price||''}}</td></tr>
        <tr><td class="table-td-title">排序号</td><td width="465">{{d.orderno||''}}</td></tr>
        <tr><td class="table-td-title">备注</td><td width="465">{{d.bz||''}}</td></tr>
    </table>

    <br>
</script>

<!-- 编辑 -->
<script type="text/html" id="editForm">
    <form class="layui-form layui-form-pane">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-md12">
                        <div class="layui-form-item"><label class="layui-form-label">活动代码</label><div class="layui-input-block"><input type="text" name="hddm" autocomplete="off" placeholder="" class="layui-input" value="{{d.hddm||''}}"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">活动名称</label><div class="layui-input-block"><input type="text" name="hdmc" autocomplete="off" placeholder="" class="layui-input" value="{{d.hdmc||''}}"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">奖项设置</label><div class="layui-input-block"><input type="text" name="jxsz" autocomplete="off" placeholder="" class="layui-input" value="{{d.jxsz||''}}"></div></div>
                        <!--<div class="layui-form-item"><label class="layui-form-label">活动周期</label><div class="layui-input-block"><input type="text" name="hdzq" autocomplete="off" placeholder="" class="layui-input" value="{{d.hdzq||''}}"></div></div>-->
                        <!--<div class="layui-form-item"><label class="layui-form-label">积分名次</label><div class="layui-input-block"><input type="text" name="jfmc" autocomplete="off" placeholder="" class="layui-input" value="{{d.jfmc||''}}"></div></div>-->
                        <div class="layui-form-item"><label class="layui-form-label">人数</label><div class="layui-input-block"><input type="text" name="pnum" autocomplete="off" placeholder="" class="layui-input" value="{{d.pnum||''}}"></div></div>
                        <!--<div class="layui-form-item"><label class="layui-form-label">发放时间（日）</label><div class="layui-input-block"><input type="text" name="fftime" autocomplete="off" placeholder="" class="layui-input" value="{{d.fftime||''}}"></div></div>-->
                        <div class="layui-form-item"><label class="layui-form-label">价值</label><div class="layui-input-block"><input type="text" name="price" autocomplete="off" placeholder="" class="layui-input" value="{{d.price||''}}"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">排序号</label><div class="layui-input-block"><input type="text" name="orderno" autocomplete="off" placeholder="" class="layui-input" value="{{d.orderno||''}}"></div></div>
                        <div class="layui-form-item"><label class="layui-form-label">备注</label><div class="layui-input-block"><input type="text" name="bz" autocomplete="off" placeholder="" class="layui-input" value="{{d.bz||''}}"></div></div>
                    </div>
                    <div class="hidden-attribute">
                        <button class="layui-btn submit" lay-submit="" lay-filter="update">新增</button>
                        <button class="layui-btn reset" type="reset">重置</button>
                    </div>
                </div>
                <input class="hidden-attribute" name="id" value="{{d.id}}" title="ID">
            </div>
        </div>
    </form>
</script>

<!-- 导入xls -->
<script type="text/html" id="importXlsForm">
    <!--    <form class="layui-form layui-form-pane">-->
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <div class="layui-form-item">
                    <label class="layui-form-label">xls文件</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="cfnum">
                        <input type="text" name="gjurl" autocomplete="off" placeholder="选择xls文件" class="layui-input" readonly="readonly" style="width:455px;float: left">
                        <button type="button" class="layui-btn" id="uploadbtn_update" style="float: left;text-align: center;width: 90px">
                            <i class="layui-icon">&#xe67c;</i>上传
                        </button>
                    </div>
                </div>
                <!--                    <div class="hidden-attribute">-->
                <!--                        <button class="layui-btn submit" lay-submit="" lay-filter="importxls_add">导入</button>-->
                <!--                        <button class="layui-btn reset" type="reset">重置</button>-->
                <!--                    </div>-->
            </div>
            <!--<input class="hidden-attribute" name="avatar">-->
        </div>
    </div>
    <!--    </form>-->
</script>

<!-- JavaScript -->
<script type="text/javascript" src="/lib/layuiadmin/layui/layui.js"></script>
<script type="text/javascript" src="/lib/layuiadmin/treeSelect/treeSelect.js"></script>

<script type="text/javascript" th:inline="none">
    layui.config({
        base: '/lib/layuiadmin/' //静态资源所在路径
    }).extend({
        treeSelect: "treeSelect/treeSelect",
        index: 'lib/index' //主入口模块
    }).use(['laydate','treeSelect','index', 'upload'], function() {
        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            upload = layui.upload,
            element = layui.element,
            laytpl = layui.laytpl,
            treeSelect = layui.treeSelect,
            $ = layui.jquery,
            // $addBtn = $('#addBtn'),
            $queryForm = $('#queryForm');
        laydate.render({elem: '#ny',type: 'month',format: 'yyyyMM'});
        var layerIndex = 1;
        var layerInitWidth;
        var layerInitHeight;

        // 绘制表格
        table.render({
// 		id: 'table',
            elem: '#table',
            url: '/question/p-djjrule/table',
            page: true, // 开始分页
            // cellMinWidth: 80, // 定义全局最小宽度
            request: {
                pageName: 'offset'
            },
            response: {
                statusCode: 10000
                // countName: 'total', //数据总数的字段名称，默认：count
                // dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[
                {type: 'numbers', title: '序号',fixed: 'left'},
                {field:'hddm', title: '活动代码', width:100},
                {field:'hdmc', title: '活动名称', width:100},
                {field:'jxsz', title: '奖项设置', width:100},
                // {field:'hdzq', title: '活动周期', width:200},
                // {field:'jfmc', title: '积分名次', width:200},
                {field:'pnum', title: '人数', width:100},
                // {field:'fftime', title: '发放时间（日）', width:200},
                {field:'price', title: '价值', width:100},
                {field:'orderno', title: '排序号', width:100},
                {field:'bz', title: '备注', width:200},
                // {field:'insertime', title: '创建时间', align:'center',width:170},
                // {field:'updatetime', title: '修改时间', align:'center',width:170},
                // {field:'userid', title: '录入账号', align:'center', width:150},
                {fixed:'right', align:'center', width: 180, toolbar: '#operationBar'}
            ]]
        });
        //监听工具条
        table.on('tool(table)', function(obj) {
            var data = obj.data,
                $this = $(this),
                getView = view.innerHTML,
                getEdit = editForm.innerHTML;
            if(obj.event === 'detail') {
                $.get('/question/p-djjrule/' + data.id, function(res) {
                    if(res.code == 403) {
                        layer.msg(res.message, {icon: 4});
                    } else {
                        laytpl(getView).render(res, function(html) {
                            popupWin($this.text(), html, false);
                        })
                    }
                })
            } else if(obj.event === 'del') {
                layer.confirm('真的删除行么', {icon: 3}, function(index) {
                    operate('DELETE', {'id': data.id}, false, obj);
                    layer.close(index);
                });
            } else if(obj.event === 'edit') {
                $.get('/question/p-djjrule/' + data.id, function(res) {
                    if(res.status == 403) {
                        layer.msg(res.message, {icon: 4});
                    } else {
                        laytpl(getEdit).render(res, function(html) {
                            popupWin($this.text(), html, true);
                        });
                        rendering();
                    }
                })
            }
        });

        // 监听状态改变
        // form.on('switch(switchSysTaskStatus)', function(obj) {
        // 	operate('PUT', {'id': this.id, 'taskstatus': obj.elem.checked}, true);
        // });
        // form.on('switch(switchTriggerStatus)', function(obj) {
        // 	operate('PUT', {'id': this.id, 'triggerstatus': obj.elem.checked}, true);
        // });
        // form.on('switch(switchTriggerType)', function(obj) {
        // 	operate('PUT', {'id': this.id, 'triggertype': obj.elem.checked}, true);
        // });

        // 搜索条件提示
        $queryForm.find('input').on('mouseover', function() {
            if(typeof($(this).attr('title')) == 'undefined') return;
            layer.tips($(this).attr('title'), this, {tips: 1});
        });

        // 新增
        form.on('submit(addBtn)', function(data) {
            var $this = $(this);
            laytpl(addForm.innerHTML).render(data, function(html) {
                popupWin($this.attr('title'), html, true);
            });
            rendering();
            return false;
        });
        // // 开启任务
        form.on('submit(enableJobBtn)', function(data) {

            // alert($("#ny").val());
            var url = '/question/p-djjrule/doTask';
            $.ajax({
                url: url,
                type: 'POST',
                async: true,
                data: {"ny":$("#ny").val()},
                success: function(res) {
                    layer.msg(res.message, {icon: 1});
                }
            })
            return false;

        });

        // 新增
        form.on('submit(add)', function(data) {
            var $form = $(this).parents('form');
            operate('POST', data.field, true);
            return false;
        });

        // 更新
        form.on('submit(update)', function(data) {
            var $form = $(this).parents('form');
            var roles = [];
            $form.find('input[name=roles]:checked').each(function() {
                roles.push($(this).val());
            });
            data.field.roles = roles;
            operate('PUT', data.field, true);
            return false;
        });

        // 查询
        form.on('submit(query)', function(data) {
            table.reload('table', {
                where: data.field
            });
            return false;
        });

        //导入 importxls
        form.on('submit(importxls)', function(data) {
            //static/lib/layuiadmin/layui/css/modules/layer/default/icon.png
            // layer.alert("importxls", {icon: 1});

            var $this = $(this);
            laytpl(importXlsForm.innerHTML).render(data, function(html) {
                popupWin($this.attr('title'), html, false);
            });
            rendering('1');
            return false;
        });

        //导出 exportxls
        form.on('submit(exportxls)', function(data) {
            // window.location.href = "/xxx/xxx/exportxls?zzlx="+zzlx;
            return false;
        });

        // 操作
        // ajaxType 请求类型
        // data 参数
        // refresh 是否刷新
        // 删除操作时传入的当前行对象
        function operate(ajaxType, data, refresh, delRow) {
            var url = '/question/p-djjrule/';
            if(ajaxType == 'DELETE') {
                url = '/question/p-djjrule/' + data.id;
            }
            $.ajax({
                url: url,
                type: ajaxType,
                async: true,
                data: data,
                success: function(res) {
                    if(res.code == 10000) {
                        layer.close(layerIndex);
                        if(refresh) {
                            table.reload('table');
                        } else if(ajaxType == 'DELETE') {
                            delRow.del();
                        }
                        if(data.resetPwd) {
                            layer.alert(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 1});
                        }
                    }  else if(res.code == 403) {
                        layer.msg(res.message, {icon: 4});
                    } else {
                        layer.msg(res.message, {icon: 5});
                    }
                }
            })
        }

        //渲染弹框
        function popupWin(title, content, showBtn) {
            layerIndex = layer.open({
                id: 'meatball',
                type: 1, //Page层类型
                area: '700px', //['700px', '480'],
                maxWidth: 1000,
                maxHeight: 800,
                title: title,
                skin: 'layui-layer-rim', //加上边框
                shade: 0.1, //遮罩透明度
                // maxmin: true, //允许全屏最小化
                anim: 0, //0-6的动画形式，-1不开启
                scrollbar: false, //禁用滚动
                content: content,
                btn: showBtn ? ['<i class="layui-icon layui-icon-ok"></i> 提交', '<i class="layui-icon layui-icon-refresh-1"></i> 重置'] : '',
                yes: function(index, layero) {
                    layero.find('form .submit').trigger('click');
                    return false;
                },
                btn2: function(index, layero) {
                    layero.find('form .reset').trigger('click');
                    return false;
                },
                success: function (layero, index) {
                    //获取当前弹出窗口的索引及初始大小
                    layerIndex = index;
                    layerInitWidth = $("#layui-layer" + layerIndex).width();
                    layerInitHeight = $("#layui-layer" + layerIndex).height();
                    resizeLayer(layerIndex, layerInitWidth, layerInitHeight);
                    form.render();
                }
            });
            // layer.full(layerIndex);
        }

        // 动态渲染控件
        function rendering(para) {
            var $form = $('form');
            // 渲染select
            form.render('select');
            // 重新渲染多选框
            form.render('checkbox');
            // 初始化日期
            // laydate.render({elem: '#ny'});
            if(para!='1'){
            }
        }

        function resizeLayer(layerIndex, layerInitWidth, layerInitHeight) {
            var windowWidth = $(document).width();
            var windowHeight = $(document).height();
            var minWidth = layerInitWidth > windowWidth ? windowWidth : layerInitWidth;
            var minHeight = layerInitHeight > windowHeight ? windowHeight : layerInitHeight;
            // console.log("win:", windowWidth, windowHeight);
            // console.log("lay:", layerInitWidth, layerInitHeight);
            // console.log("min:", minWidth, minHeight);
            layer.style(layerIndex, {
                top: 15,
                width: minWidth,
                height: minHeight
            });
        }
        $(window).resize(function() {
            resizeLayer(layerIndex,layerInitWidth,layerInitHeight);
        });

    });
</script>
</body>
</html>